package com.unbook.controller;

import com.unbook.entity.BookType;
import com.unbook.entity.ResponseVo;
import com.unbook.service.IBookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Null;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@RestController
@RequestMapping("/bookType")
public class BookTypeController {
    @Autowired
    private IBookTypeService bookTypeService;

    @GetMapping("/list")
    public ResponseVo<List<BookType>> getBookList(
        @RequestParam(required = false, defaultValue = "") String name
    ) {
        return ResponseVo.success(bookTypeService.list());
    }

    @PostMapping("/insert")
    public ResponseVo<BookType> insertBook(@RequestBody BookType body){
        bookTypeService.save(body);
        return ResponseVo.success(body);
    }

    @DeleteMapping("/deleteByIds")
    public ResponseVo<Null> deleteBook(@RequestParam String bIds){
        List<Integer> appIdList = Stream.of(bIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        return bookTypeService.removeByIds(appIdList) ? ResponseVo.success(null) : ResponseVo.deleteErr();
    }

    @PutMapping("/update")
    public ResponseVo<Null> updateBook(@RequestBody BookType body){
        return bookTypeService.updateById(body) ? ResponseVo.success(null) : ResponseVo.updateErr();
    }
}
